﻿module CalculationCE =

    type CalcState =
        { r: int }

    type CalculationBuilder() =
        member _.Yield _ =
            { r = 0 }

        [<CustomOperation "add">]
        member _.Add(state: CalcState, x) =
            { state with
                r = state.r + x }

        [<CustomOperation "sub">]
        member _.Sub(state: CalcState, x) =
            { state with
                r = state.r - x }

        [<CustomOperation "mul">]
        member _.Mul(state: CalcState, x) =
            { state with
                r = state.r * x }
        
        [<CustomOperation "div">]
        member _.Div(state: CalcState, x) =
            if x = 0 then
                failwith "can't divide by 0"
            { state with
                r = state.r / x }

    let calculation = CalculationBuilder()

open CalculationCE

let c =
    // 500 applications of custom operations
    calculation {
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
        add 1
        sub 1
        mul 1
        div 1
    }
